package scm;

import java.io.*;
import java.net.*;
import javax.swing.*;

public class TcpClient
{
     private Integer port;
     private String ip;
     private Socket clientSocket;
     private BufferedReader inFromServer;

     public TcpClient(String ip, Integer port)
     {
         this.port = port;
         this.ip = ip;
     }

     public void start()
     {
         try
         {
             clientSocket = new Socket(ip, port);

         }
         catch (UnknownHostException ex)
         {
             JOptionPane.showMessageDialog(null, ex.getMessage());
         }
         catch (IOException ex)
         {
             JOptionPane.showMessageDialog(null, ex.getMessage());
         }
     }

     public String read()
     {
         try
         {
             ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
             String odczyt = ois.readObject().toString();
             ois.close();
             return odczyt;
         }
         catch (IOException ex)
         {
             JOptionPane.showMessageDialog(null, ex.getMessage());
             return null;
         }
         catch (ClassNotFoundException ex)
         {
             JOptionPane.showMessageDialog(null, ex.getMessage());
             return null;
         }
     }

     public void close()
     {
         try
         {
             clientSocket.close();
         }
         catch (IOException ex)
         {
             JOptionPane.showMessageDialog(null, ex.getMessage());
         }
     }
}
